<template>
	<!-- 评论列表 -->
	<view style="width: 100%;height: 100vh;background-color: white;">
		<commentListVue :dataList="dataList" :showReply="showReply" :bootId="bootId" :userId="userId" :token="token"
			ref="commentList" @refreshMySingleReply="refreshMySingleReply">
		</commentListVue>
	</view>
</template>

<script>
	import commentListVue from './comment-list.vue';

	import {
		getMyDynamicCommentByType,
	} from '@/api/home.js';
	export default {
		components: {
			commentListVue
		},
		props: {
			userId: {
				type: String,
				required: true,
				default: ''
			},
			token: {
				type: String,
				required: true,
				default: ''
			},
			bootId: {
				type: String,
				required: true,
				default: ''
			},
		},
		data() {
			return {
				dataList: [],
				showReply: '0',
				page: 1,
				hasMore: true,
			}
		},
		mounted() {
			let that = this;
			that.getComment();
		},
		created() {
			uni.$on('refreshCommentList', this.refreshList);
		},
		beforeDestroy() {
			uni.$off('refreshCommentList'); // 避免内存泄漏
		},
		methods: {
			refreshMySingleReply(e) {
				let that = this;
				let rootId = e.rootId;
				let replaceindex = e.replyIndex;
				let singleIndex = Math.floor(replaceindex / 20) + 1;
				console.log('mysingleIndex===   ', singleIndex);
				console.log('myrootId===   ', rootId);
				console.log('myreplaceindex===   ', replaceindex);
				console.log('myreplaceindex===   ', that.dataList.length);
				getMyDynamicCommentByType({
					page: singleIndex,
					dynId: that.bootId,
					type: '训练营',
					userId: that.userId,
					token: that.token,
				}).then(res => {
					let arr = res.data.mess;

					arr.forEach((item, index) => {
						if (item.id == rootId) {
							that.$set(that.dataList, replaceindex, item);
						}
					});

				})
			},
			bottomRequest() {
				console.log('getMyDynamicCommentByType执行了');
				let that = this;
				that.page++;
				if (that.hasMore == true) {
					that.getComment();
				}
			},
			refreshList() {
				let that = this;
				console.log('&&&&  ----')
				that.page = 1;
				that.hasMore = true;
				that.getComment();
			},
			getComment() {
				let that = this;
				getMyDynamicCommentByType({
					page: that.page,
					dynId: that.bootId,
					type: '训练营',
					userId: that.userId,
					token: that.token,
				}).then(res => {
					let arr = res.data.mess;
					if (arr.length < 20) {
						that.hasMore = false;
					}
					if (that.page == 1) {
						that.dataList = [];
					}

					arr.forEach((item, index) => {
						that.dataList.push(item)
					});

					that.dataList.forEach(item => {
						that.$set(item, 'showMore', '0');
					});
					//console.log(JSON.stringify(that.dataList))
				})
			},
		},
	}
</script>

<style lang="scss" scoped>

</style>